﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;
using System.IO;

using Microsoft.Office.Core;

namespace ConvertOffice
{
    public class Excel2Html
    {
        public void Excute(string srcPath, string targetDir)
        {
            Microsoft.Office.Interop.Excel.Application app = null;
            Microsoft.Office.Interop.Excel.Workbooks workbooks = null;
            Microsoft.Office.Interop.Excel.Workbook workbook = null;

            try
            {
                app = new Microsoft.Office.Interop.Excel.Application();
                workbooks = app.Workbooks;

                bool readOnly = false;
                workbook = workbooks.Open(srcPath,
                                            Type.Missing,
                                            readOnly,
                                            Type.Missing,
                                            Type.Missing,
                                            Type.Missing,
                                            Type.Missing,
                                            Type.Missing,
                                            Type.Missing,
                                            Type.Missing,
                                            Type.Missing,
                                            Type.Missing,
                                            Type.Missing,
                                            Type.Missing,
                                            Type.Missing);
                //如果文档有密码
                if (workbook.HasPassword || workbook.ProtectStructure || workbook.ProtectWindows)
                    throw new Exception("文档有保护限制");

                //word另存为html后，html文件名
                string htmlFileName = targetDir + Path.DirectorySeparatorChar + "index.html";
                //word另存为html后，文件夹名
                string htmlFolder = targetDir + Path.DirectorySeparatorChar + "index.files";
                //存放另存为的html文件夹是否存在
                if (!Directory.Exists(targetDir))
                    Directory.CreateDirectory(targetDir);

                // 另存为 html
                workbook.WebOptions.Encoding = MsoEncoding.msoEncodingUTF8;
                workbook.SaveAs(htmlFileName,
                            Microsoft.Office.Interop.Excel.XlFileFormat.xlHtml,
                            Type.Missing,
                            Type.Missing,
                            readOnly,
                            Type.Missing,
                            Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlNoChange,
                            Type.Missing,
                            Type.Missing,
                            Type.Missing,
                            Type.Missing,
                            Type.Missing);
            }
            finally
            {
                if (workbook != null)
                {
                    workbook.Close(Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlNoChange, Type.Missing, Type.Missing);
                    Marshal.ReleaseComObject(workbook);
                    workbook = null;
                }
                if (workbooks != null)
                {
                    workbooks.Close();
                    Marshal.ReleaseComObject(workbooks);
                    workbooks = null;
                }
                if (app != null)
                {
                    app.Quit();
                    Marshal.ReleaseComObject(app);
                    app = null;
                }
                GC.Collect();
                GC.WaitForPendingFinalizers();
            }
        }
    }
}
